raise TypeError("%r.connect argument must be <type 'tuple'> (type, addr) not %r (%r)", type(self).__name__, type(host_port), host_port)
bad_addr = False
port = None
if len(addr) == 1:
host = addr[0]
port = 80
elif isinstance(addr, basestring):
(host, port) = util.srv_str_to_tuple(addr, 80)
elif len(addr) == 2:
(host, port) = addr
else:
bad_addr = True
try:
port = int(port)
except ValueError:
bad_addr = True
if bad_addr:
raise TypeError('%r.connect argument\'s second element must be either string ("srv" or "srv:port") or tuple (("srv", port) or ("srv",)).Got %r instead' % addr)
return (type, host)
def connect_args_for(self, type, addr):
return ((type.upper(), addr),)
def _poll(self):
if not self._waiting:
self.send(None)
def pause(self):
self._paused = True
def unpause(self):
self._paused = False
common.netcall(self.process)
def send(self, msgobj, trid = sentinel, callback = None, **kw):
self._q.append((msgobj, trid, callback, kw))
if not self._paused:
common.netcall(self.process)
send = util.callsback(send)
def process(self):
if not (self._q) or self._waiting:
return None
self._waiting = True
data = []
sending = []
queue = self._q[:]
self._q[:] = []
while queue:
(msgobj, trid, callback, kw) = queue.pop(0)
if msgobj is not None:
self.set_trid(msgobj, trid)
self.set_callbacks(msgobj, callback)
data.append(str(msgobj))
sending.append(callback)
if self._session_id is None:
url_kws = dict(Action = 'open', Server = self.type, IP = self.host)
close = session_info.get('Session', '').lower() == 'close'
if self._session_id is None and not close:
raise Exception("Didn't get a session ID!")
self._waiting = False
if not close:
common.netcall(self.process)
if close:
self._session_id = None
data = response.body
self._process_data(data)
if close:
self.on_close()
def _process_data(self, data):
line = data.readline()
while line:
payload = False
line.rstrip('\r\n')
dlist = line.split()
cmd = dlist[0]
if cmd in self.payload_commands:
payload = True
try:
sz = int(dlist[-1])
except ValueError:
sz = 0
line += data.read(sz)
try:
msg = msn.Message.from_net(line, payload)
self.on_message(msg)
except Exception:
e = None
log.error('Error handling %r. e = %r', line, e)
traceback.print_exc()
line = data.readline()
def _on_send_error(self, e):
log.error('Something bad happened in MsnHttpSocket: %r', e)
self.on_conn_error(e)
def make_request(self, url_kws, data = None):
url = util.UrlQuery(self.endpoint, url_kws)
headers = {
'Accept': '*/*',
'Content-Type': 'text/xml; charset=utf-8',
'User-Agent': 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; SIMBAR={0B74DA00-76D2-11DD-9ABA-0016CFF93348}; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 3.0.04506; .NET CLR 3.5.21022; .NET CLR 1.1.4322; Windows Live Messenger BETA 9.0.1407)',